#include "register.h"
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QDebug>
#include <QFile>
#include <QDir>
extern LinkList<Node>stu;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("登陆界面");

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter p(this);//创建画家对象p
    //绘图操作
    p.drawPixmap(rect(),QPixmap(":/new/prefix1/IMG_20210819_144344.jpg"));
}




void Widget::on_loginButton_clicked()
{
    QString username=ui->username->text();
    QString password=ui->password->text();
    static int count = 0;
    //若用户名与密码不为空
     qDebug()<<QDir::currentPath();
    if((username!=NULL) && (password!=NULL))
    {
        //在工作目录创建文件
        QFile file(QCoreApplication::applicationDirPath()+"/date.txt");
        if(file.exists())
        {
           qDebug()<<"文件存在";
        }
        else{
            qDebug()<<"文件不存在,正在新建文件.";
            file.open( QIODevice::ReadWrite | QIODevice::Text );
        }
        file.close();

        file.open(QIODevice::ReadOnly | QIODevice::Text);

        qDebug()<<"正在检查是否是否有匹配的id和密码";
        //创建文件读取流
        QTextStream in(&file);
        //遍历账号密码
        while(!in.atEnd())
        {
            //设置编码方式为“UTF-8”
             in.setCodec("UTF-8");
             QString str= in.readLine();
             if(str=="\n")
             {
                 continue;
             }
             QStringList list = str.split(",");
             //密码
             QString dtn = list[0];
             QString dtp = list[1];
             if((dtn == username ) && (dtp == password))
             {
                 qDebug()<<"已找到";
                 QMessageBox::information(this,"恭喜","登录成功！");
                 //然后跳转新窗口
                 mainWindow *win = new mainWindow;
                 win->show();
                 this->close();
                 count++;
                 break;

             }
        }
        if(count == 0)
        {
            QMessageBox::critical(this,"错误！","密码或账号有误！");
        }
        file.close();
    }
    else
    {
        QMessageBox::critical(this,"错误！","密码为空");
    }
}

void Widget::on_registerButton_clicked(){
    Register*a=new Register;
    a->setFixedSize(530,630);
    a->show();
    this->close();
}
